package com.clock.web.controller.shop;

import com.clock.common.annotation.Log;
import com.clock.common.core.controller.BaseController;
import com.clock.common.core.domain.AjaxResult;
import com.clock.common.core.page.TableDataInfo;
import com.clock.common.enums.BusinessType;
import com.clock.common.enums.shop.EmployeeStatus;
import com.clock.common.utils.poi.ExcelUtil;
import com.clock.shop.domain.ShopEmployee;
import com.clock.shop.domain.ShopShop;
import com.clock.shop.domain.vo.EmployeeVo;
import com.clock.shop.service.IShopEmployeeService;
import com.clock.shop.service.IShopShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 职工Controller
 * 
 * @author ruoyi
 * @date 2024-10-11
 */
@RestController
@RequestMapping("/shop/employee")
public class ShopEmployeeController extends BaseController
{
    @Autowired
    private IShopEmployeeService shopEmployeeService;
    @Autowired
    private IShopShopService shopShopService;
    /**
     * 查询职工列表
     */
    // @PreAuthorize("@ss.hasPermi('shop:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmployeeVo shopEmployee)
    {
        startPage();
        List<ShopShop> shopList = shopShopService.selectShopShopByShopName(shopEmployee.getShopName());
        HashMap<Long, String> map = new HashMap<>();
        for (ShopShop shop:shopList) {
            map.put(shop.getShopId(),shop.getShopName());
        }
        // List<ShopEmployee> list=new ArrayList<>();
        // for (ShopShop shopShop : shopList) {
        //     ShopEmployee shopEmployee1 = new ShopEmployee();
        //     BeanUtils.copyProperties(shopEmployee, shopEmployee1);
        //     shopEmployee1.setShopId(shopShop.getShopId());
        //     list.add(shopEmployeeService.selectShopEmployeeList(shopEmployee1));
        // }
        ShopEmployee shopEmployee1 = new ShopEmployee();
        BeanUtils.copyProperties(shopEmployee, shopEmployee1);
        if(shopEmployee.getShopName()!=null){
            shopEmployee1.setShopId(shopList.get(0).getShopId());
        }
        List<ShopEmployee> list = shopEmployeeService.selectShopEmployeeList(shopEmployee1);
        ArrayList<EmployeeVo> employeeVos = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            // ShopShop shopShop1 = shopShopService.selectShopShopByShopId(list.get(i).getShopId());
            EmployeeVo employeeVo = new EmployeeVo();
            BeanUtils.copyProperties(list.get(i),employeeVo);
            employeeVo.setShopName(map.get(list.get(i).getShopId()));
            if(list.get(i).getEmployeeStatus()== EmployeeStatus.FREE.getCode()){
                employeeVo.setEmployeeStatus(EmployeeStatus.FREE.getInfo());
            }else if(list.get(i).getEmployeeStatus()== EmployeeStatus.WORKING.getCode()){
                employeeVo.setEmployeeStatus(EmployeeStatus.WORKING.getInfo());
            }else {
                employeeVo.setEmployeeStatus(EmployeeStatus.ENDWORK.getInfo());
            }
            employeeVos.add(employeeVo);
        }
        return getDataTable(employeeVos);
    }

    /**
     * 导出职工列表
     */
    @PreAuthorize("@ss.hasPermi('shop:employee:export')")
    @Log(title = "职工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopEmployee shopEmployee)
    {
        List<ShopEmployee> list = shopEmployeeService.selectShopEmployeeList(shopEmployee);
        ExcelUtil<ShopEmployee> util = new ExcelUtil<ShopEmployee>(ShopEmployee.class);
        util.exportExcel(response, list, "职工数据");
    }

    /**
     * 获取职工详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:employee:query')")
    @GetMapping(value = "/{employeeId}")
    public AjaxResult getInfo(@PathVariable("employeeId") Long employeeId)
    {
        return success(shopEmployeeService.selectShopEmployeeByEmployeeId(employeeId));
    }

    /**
     * 新增职工
     */
    @PreAuthorize("@ss.hasPermi('shop:employee:add')")
    @Log(title = "职工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmployeeVo employeeVo)
    {

        return toAjax(shopEmployeeService.insertShopEmployee(employeeVo));
    }

    /**
     * 修改职工
     */
    @PreAuthorize("@ss.hasPermi('shop:employee:edit')")
    @Log(title = "职工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopEmployee shopEmployee)
    {
        return toAjax(shopEmployeeService.updateShopEmployee(shopEmployee));
    }

    /**
     * 删除职工
     */
    @PreAuthorize("@ss.hasPermi('shop:employee:remove')")
    @Log(title = "职工", businessType = BusinessType.DELETE)
	@DeleteMapping("/{employeeIds}")
    public AjaxResult remove(@PathVariable Long[] employeeIds)
    {
        return toAjax(shopEmployeeService.deleteShopEmployeeByEmployeeIds(employeeIds));
    }
}
